package com.aaa.security;

import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * @ClassName: AuthFailHandler
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/7/28 22:40
 * @Version:1.0
 **/
@Component
public class AuthFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        HashMap<Object, Object> map = new HashMap();
        map.put("code",HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        map.put("msg","认证失败");
        map.put("data","f");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.println(JSON.toJSONString(map));
        writer.flush();
        writer.close();
    }
}
